package cn.gjxblog.controller;

import cn.gjxblog.common.model.HttpResult;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author gjx
 * @version v1.0
 * @Description
 * @date 2019/4/24 8:46
 */
@RequestMapping("/sms")
@RestController
public class SMSController {
    // 短信应用SDK AppID
    int appid = 1400113701; // 1400开头

    // 短信应用SDK AppKey
    String appkey = "e8bf3e3431cea0bcb24c3990f89371b0";

    //templateId7839对应的内容是"您的验证码是: {1}"
    // 签名
    String smsSign = "腾讯云";
    // 短信模板ID，需要在短信应用中申请
    int templateId = 156972; // NOTE: 这里的模板ID`7839`只是一个示例，真实的模板ID需要在短信控制台中申请


    @GetMapping("/sendSms")
    public HttpResult sendSms(String code,String phone){
        // 需要发送短信的手机号码
        String[] phoneNumbers = {phone};
        try {
            String[] params = {code};
            SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
            SmsSingleSenderResult result = ssender.sendWithParam("86", phoneNumbers[0],
                    templateId, params, smsSign, "", "");
            System.out.println(result);
            return HttpResult.success();
        } catch (HTTPException e) {
            // HTTP响应码错误
            e.printStackTrace();
        } catch (JSONException e) {
            // json解析错误
            e.printStackTrace();
        } catch (IOException e) {
            // 网络IO错误
            e.printStackTrace();
        }
        return HttpResult.error();
    }



}
